<!DOCTYPE html>
<html lang="en">

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="format-detection" content="telephone=no">
		<title>51微贷</title>
		<link rel="stylesheet" type="text/css" href="../css/reset.css">
		<link rel="stylesheet" type="text/css" href="../css/common.css">
		<link rel="stylesheet" type="text/css" href="../css/icomoon/style.css">
		<script type="text/javascript" src="../js/rem.js"></script>
	</head>

	<body>
		<div class="m-common m-securitypwd">
			<div class="hd hd-1">
				<h3 class="title f-pr">
					<span>交易密码</span>
					<i class="icon-angle-left f-pa"></i>
				</h3>
				<div class="avator"><img src="../images/password.png"></div>
				<p>您未设置交易密码</p>
				<p class="small">在账户资金变动、修改账户信息时需要输入的密码</p>
			</div>
			<ul>
				<li>
					<label>验证码</label>
					<input type="text" class="code" placeholder="请输入验证码">
					<button class="code-text f-pa">获取验证码</button>
					<i class="close-icon f-pa"></i>
				</li>
			</ul>
			<ul class="mt">
				<li>
					<label>密码</label>
					<input type="password" class="password" placeholder="含字母和数字的6-16位字符">
					<i class="close-icon f-pa"></i>
					<i class="unsee-icon f-pa"></i>
				</li>
				<li>
					<label>确认密码</label>
					<input type="password" class="confirmpwd" placeholder="再次输入密码">
					<i class="close-icon f-pa"></i>
					<i class="unsee-icon f-pa"></i>
				</li>
			</ul>
			<div class="btn-box">
				<a class="btn">设置交易密码</a>
			</div>
		</div>
		<script type="text/javascript" src="../js/zepto.min.js"></script>
		<script type="text/javascript" src="../js/mui.min.js"></script>
		<script type="text/javascript" src="../js/common.js"></script>

		<script type="text/javascript">
			mui.plusReady(function() {
				console.log('来源入口：' + plus.webview.currentWebview().preid);
				function countdowncode(wait) {
					var $obj = $('.code-text');
					if(wait === 0) {
						$obj.text('获取验证码').removeClass('disable').attr('disable', false);
					} else {
						$obj.text(wait + '秒后重发').addClass('disable').attr('disable', true);
						wait--;
						setTimeout(function() {
							countdowncode(wait);
						}, 1000);
					}
				}

				function validate() {
					var code = $.trim($('.code').val()),
						password = $.trim($('.password').val()),
						confirmpwd = $.trim($('.confirmpwd').val());
					if(code == '') {
						mui.toast('请输入验证码');
						return false;
					}
					if(password == '') {
						mui.toast('请输入密码');
						return false;
					} else if(password.length < 6 || password.length > 16) {
						mui.toast('密码只能是含字母和数字的6-16位字符');
						return false;
					}
					if(confirmpwd == '') {
						mui.toast('请再次输入密码');
						return false;
					}
					if(confirmpwd != password) {
						mui.toast('两次输入的密码不一致');
						return false;
					}
					return true;
				}

				$('.icon-angle-left').on('tap', function() {
					mui.back();
				});

				$('.code-text').on('tap', function() {
					mui.ajax(base + 'account/securitypasswordcode', {
						type: 'post',
						data: {
							'id': parseInt(plus.storage.getItem('user_id'))
						},
						dataType: 'json',
						success: function(data) {
							console.log('发送短信验证码：' + JSON.stringify(data));
							if(data.result) {
								countdowncode(data.wait_second);
							} else {
								mui.toast(data.message);
							}
						},
						error: function(data) {
							console.error(JSON.stringify(data));
						}
					});
				});

				$('.btn').on('tap', function() {
					if(!validate()) {
						return false;
					} else {
						mui.ajax(base + 'account/setsecuritypassword', {
							type: 'post',
							data: {
								'id': parseInt(plus.storage.getItem('user_id')),
								'code': $.trim($('.code').val()),
								'password': $.trim($('.password').val()),
								'confirm': $.trim($('.confirmpwd').val())
							},
							dataType: 'json',
							success: function(data) {
								if(data.result) {
									var preid = plus.webview.currentWebview().preid;
									if(preid != null && preid != undefined) {
										plus.webview.getWebviewById(preid).evalJS('load()');
									}
									mui.alert('交易密码设置成功', '设置交易密码', function() {
										mui.back();
									});
								} else {
									mui.toast(data.message);
								}
							},
							error: function(data) {
								console.error(data.message);
							}
						});
					}
				});
			});
		</script>
	</body>

</html>